feat(#270): require N consecutive successes in HALF_OPEN before closing circuit#350
Closed
samsolo247 wants to merge 1 commit into
Closed
Conversation
…efore closing circuit Implements issue ChainForgee#270 (backlog item ChainForgee#62). Problem: record_success() transitioned HALF_OPEN → CLOSED on the very first success, risking premature closure after a single lucky probe. Changes ------- services/circuit_breaker.py - New constructor param success_threshold_in_half_open (default 2). - New internal counter _half_open_success_count, reset to 0 whenever the breaker enters or re-enters HALF_OPEN (allow_request) and on any failure (record_failure). - record_success() in HALF_OPEN now increments the counter and only transitions to CLOSED once the threshold is reached. - record_failure() resets _half_open_success_count before reopening, ensuring the next probe window starts from zero. - All state transitions remain thread-safe (same Lock). - Docstrings updated. tests/test_circuit_breaker.py - test_circuit_breaker_basic_transitions updated: default threshold=2 means first success stays HALF_OPEN, second closes. - TestHalfOpenSuccessThreshold (new class, 6 tests): • threshold_3_two_successes_remain_half_open — AC ChainForgee#1 • threshold_3_third_success_closes — AC ChainForgee#2 • default_threshold_is_2 • threshold_1_single_success_closes (backward-compat) • failure_resets_success_count_and_reopens • allow_request_resets_count_on_reentry - TestCircuitBreakerMetrics.test_success_closes_circuit_and_resets_state_gauge updated to issue two record_success() calls (matches default threshold=2).
samsolo247
deleted the
feat/issue-270-circuit-breaker-half-open-threshold
branch
July 16, 2026 09:13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #270
Fixes premature circuit-breaker closure (backlog item #62). Previously
record_success()transitionedHALF_OPEN → CLOSEDon the very first success — a single lucky probe could reopen a broken provider. This PR adds a configurablesuccess_threshold_in_half_open(default 2) so the breaker only returns toCLOSEDafter N consecutive successes.Changes
app/ai-service/services/circuit_breaker.pysuccess_threshold_in_half_open: int = 2_half_open_success_count— reset on every OPEN→HALF_OPEN entry and on any failurerecord_success()record_failure()_half_open_success_count = 0before reopening — next probe window starts cleanallow_request()_half_open_success_count = 0on each OPEN→HALF_OPEN transitionAll state transitions remain thread-safe under the existing
Lock.app/ai-service/tests/test_circuit_breaker.py6 new tests in
TestHalfOpenSuccessThreshold:test_threshold_3_two_successes_remain_half_opentest_threshold_3_third_success_closestest_default_threshold_is_2test_threshold_1_single_success_closestest_failure_resets_success_count_and_reopenstest_allow_request_resets_count_on_reentry2 existing tests updated to reflect the new default threshold of 2.
Test run
All 15 tests pass (9 pre-existing + 6 new).